import axios from 'axios'
import qs from 'qs'

const domain = 'http://10.16.30.159:9993'
const instance = axios.create()

instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

const get = (url, params) => {
  return new Promise((resolve, reject) => {
    instance.get(`${domain}${url}`, {
      params
    })
      .then(resp => {
        resolve(resp.data)
      })
      .catch(err => {
        reject(err.message)
      })
  })
}

const post = (url, params) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: `${domain}${url}`,
      data: qs.stringify(params),
      headers: {
        "Content-Type":'application/x-www-form-urlencoded'
      },
    })
      .then(resp => {
        resolve(resp.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

const getData = (params) => {
  return new Promise((resolve, reject) => {
    for (let i in params) {
      if (typeof params[i] === 'object') {
        params[i] = JSON.stringify(params[i])
      }
    }
    post(`/appInterface/dataService.do`, params)
      .then(result => {
        resolve(result)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export default {
  get,
  post,
  getData
}
